package edu.paper.job.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import edu.paper.job.dao.CompanyDao;
import edu.paper.job.domain.Company;
import edu.paper.job.service.CompanyService;

/**
 * 企业业务逻辑实现类
 * @author bowen
 *
 */
@Service
public class CompanyServiceImpl implements CompanyService{

	//Spring bean工厂专门用来创建对象的
	@Autowired
	private ApplicationContext applicationContext;
	
	//企业数据库操作
	@Autowired
	private CompanyDao companyDao;
	
	@Override
	public List<Company> find(Company company) {
		return companyDao.find(company);
	}

	@Override
	public Company get(Serializable id) {
		return companyDao.get(id);
	}

	@Override
	public void insert(Company company) {
		companyDao.insert(company);
	}

	@Override
	public void update(Company company) {
		companyDao.update(company);
	}

	@Override
	public void deleteById(Serializable id) {
		companyDao.deleteById(id);
	}

	@Override
	public void delete(Serializable[] ids) {
		companyDao.delete(ids);
	}

	@Override
	public boolean register(Company company) {
		//登录名不能重复
		Company params = applicationContext.getBean(Company.class);
		params.setUsername(company.getUsername());
		if(companyDao.find(params).size() == 0) {
			//没查到相同的登录名，注册成功
			companyDao.insert(company);
			return true;
		}
		//查询到重复的登录名，注册失败
		return false;
	}

	@Override
	public Company login(Company company) {
		//查找账户
		List<Company> companys = companyDao.find(company);
		if(companys.size() > 0) {
			//找到账户，登录成功（只能找到一个）
			return companys.get(0);
		}
		//没找到，登录失败
		return null;
	}

}
